﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BitPortal.Utils;
using BitPortal.Business;
using BitPortal.Models;
namespace BitPortal.Modules.ProductSearch
{
    public partial class ProductSearch : Controls.BaseUserControl
    {
        public bool EnableQuickView = true;
        public bool SearchInPage = true;
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            if (ReferenceModule != null)
            {
                EnableQuickView = Convert.ToBoolean(ReferenceModule.GetModuleConfig("EnableQuickView"));
                SearchInPage = Convert.ToBoolean(ReferenceModule.GetModuleConfig("SearchInPage"));
                ltrTitle.Text = ReferenceModule.GetModuleConfig("Header").ToString();
            }
            if (EnableQuickView) (this.Page as Controls.BaseFrontendPage).IncludeScriptFile("http://gettopup.com/releases/latest/top_up-min.js");
            //Load Filters
            ProductCategory currentCate = ProductCategoryBLL.GetByID(GeneralUtils.ToInt32TryParse(Request.QueryString["ProductCategoryID"]));
            List<ProductProperty> listProperties = ProductPropertyBLL.GetPropertiesForCategory(currentCate);
            string filter = "{\"Filter\":[";
            foreach (ProductProperty p in listProperties)
            {
                filter += "{ \"Name\": \"" + p.Name + "\", \"ID\": " + p.PropertyID + ", \"Type\": \"" + p.DataType + "\" },";
            }
            filter += "]}";
            (this.Page as Controls.BaseFrontendPage).RegisterScript("var filters = " + filter + ";AddFilter(filters);");
        }

        protected void btnSearch_OnClick(object sender, EventArgs e)
        {
            string condition = "";
            if (txtKeyword.Text.Length > 0)
            {
                if (txtKeyword.Text.Length > 2)
                {
                    condition += "-" + txtKeyword.Text + "-Name-ctn;";
                }
                else
                {
                    condition += "-" + txtKeyword.Text + "-Name-stw;";
                }
            }
            if (txtPriceFrom.Text.Length > 0)
            {
                condition += "-" + txtPriceFrom.Text.ToInt64TryParse() + "-Price-gt;";
            }
            if (txtPriceTo.Text.Length > 0)
            {
                condition += "-" + txtPriceTo.Text.ToInt64TryParse() + "-Price-lt;";
            }
            if (ckIsHot.Checked) condition += "-true-IsHot-eq;";
            if (ckIsNew.Checked) condition += "-true-IsNew-eq;";
            if (ckIsDiscount.Checked) condition += "-true-IsDiscount-eq;";
            Response.Redirect(URLUtils.AddParamToQueryString("c", condition));
        }
    }
}